package com.woniu.market_stall.web;



import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.Grade;
import com.woniu.market_stall.entity.User;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.GradeService;
import com.woniu.market_stall.vo.GradeVo;
import com.woniu.market_stall.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("grade")
public class Usergradecontroller {

    

    @Autowired
    GradeService gradeService;

    @GetMapping("/list")
    public ResultResponse<PageInfo<GradeVo>> rlist(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                  @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                  String name){
        ResultResponse<PageInfo<GradeVo>> result = null;

        try {
            PageInfo<GradeVo> rolePageInfo = gradeService.queryByPage(now, size, name);
            result = new ResultResponse<>(200,"查询成功!",rolePageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }



    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody GradeVo gradeVo){
        ResultResponse<Void> result = null;

        try {
            Grade gradeuu = gradeService.queryGradeByKey(gradeVo.getId());
            if(gradeVo.getDefaultState()==0){
                gradeuu.setGrade(gradeVo.getGrade());
                gradeuu.setDefaultState(gradeVo.getDefaultState());
                gradeService.modifyGrade(gradeuu);
            }else {
                gradeuu.setGrade(gradeVo.getGrade());
                gradeService.modify(gradeuu);
            }
            //根据 ID 查询出来 当前数据  把前台传递过来的数据  赋值到新查询出来的对象上
            result = new ResultResponse<>(200,"修改成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改失败!");
        }
        return result ;
    }
    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody GradeVo gradeVo){
        ResultResponse<Void> result = null;

        try {
            int i =gradeVo.getDefaultState();
            if(i == 1){
                gradeService.add(gradeVo);
            }else {
                gradeService.addGrade(gradeVo);

            }

            result = new ResultResponse<>(200,"添加成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加失败!");
        }
        return result ;
    }
    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id") long id){
        ResultResponse<Void> result = null;

        try {
            gradeService.removeStallByKey(id);
            result = new ResultResponse<>(200,"删除成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除失败!");
        }
        return result ;
    }



}
